﻿namespace Alkitab
{
    using Microsoft.Phone.Controls;
    using System;
    using System.Collections.ObjectModel;
    using System.IO;
    using System.Windows;
    using System.Windows.Controls;

    public partial class ParallelRead : PhoneApplicationPage
    {
        #region Fields

        private int ayatke;
        private int ijudul;

        #endregion Fields

        #region Constructors

        public ParallelRead()
        {
            InitializeComponent();
            bacaan1.FontSize = (int)Globals.Settingan["fontsize?"];
            bacaan2.FontSize = (int)Globals.Settingan["fontsize?"];
            bacaan3.FontSize = (int)Globals.Settingan["fontsize?"];
            bacaan4.FontSize = (int)Globals.Settingan["fontsize?"];
            bacaan5.FontSize = (int)Globals.Settingan["fontsize?"];

            if (Globals.terjemahanlamakah) ijudul = Globals.nobuku; else ijudul = Globals.nobuku + 39;
            bacaan1.ItemsSource = getText(ijudul, Globals.nopasal, 1, "TerjemahanBaru");
            bacaan2.ItemsSource = getText(ijudul, Globals.nopasal, 1, "IndonesiaSeharihari");
            bacaan3.ItemsSource = getText(ijudul, Globals.nopasal, 1, "GermanvLuther");
            bacaan4.ItemsSource = getText(ijudul, Globals.nopasal, 1, "AmericanStandardVersion");
            bacaan5.ItemsSource = getText(ijudul, Globals.nopasal, 1, "KingJamesVersion");
            int x = Convert.ToInt16(Globals.noayat) - 1;
            if (x < bacaan1.Items.Count) bacaan1.SelectedIndex = x; else bacaan1.SelectedIndex = x - 1;
            if (x < bacaan2.Items.Count) bacaan2.SelectedIndex = x; else bacaan2.SelectedIndex = x - 1;
            if (x < bacaan3.Items.Count) bacaan3.SelectedIndex = x; else bacaan3.SelectedIndex = x - 1;
            if (x < bacaan4.Items.Count) bacaan4.SelectedIndex = x; else bacaan4.SelectedIndex = x - 1;
            if (x < bacaan5.Items.Count) bacaan5.SelectedIndex = x; else bacaan5.SelectedIndex = x - 1;
        }

        #endregion Constructors

        #region Methods

        private void bacaan_Loaded(object sender, RoutedEventArgs e)
        {
            bacaan1.ScrollIntoView(bacaan1.SelectedItem);
            bacaan2.ScrollIntoView(bacaan2.SelectedItem);
            bacaan3.ScrollIntoView(bacaan3.SelectedItem);
            bacaan4.ScrollIntoView(bacaan4.SelectedItem);
            bacaan5.ScrollIntoView(bacaan5.SelectedItem);
        }

        private void bacaan_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender as ListBox).SelectedIndex < bacaan1.Items.Count) bacaan1.SelectedIndex = (sender as ListBox).SelectedIndex;
            if ((sender as ListBox).SelectedIndex < bacaan2.Items.Count) bacaan2.SelectedIndex = (sender as ListBox).SelectedIndex;
            if ((sender as ListBox).SelectedIndex < bacaan3.Items.Count) bacaan3.SelectedIndex = (sender as ListBox).SelectedIndex;
            if ((sender as ListBox).SelectedIndex < bacaan4.Items.Count) bacaan4.SelectedIndex = (sender as ListBox).SelectedIndex;
            if ((sender as ListBox).SelectedIndex < bacaan5.Items.Count) bacaan5.SelectedIndex = (sender as ListBox).SelectedIndex;
            ayatke = (sender as ListBox).SelectedIndex + 1;
            Globals.noayat = ayatke;
        }

        private ObservableCollection<String> getText(int kk, int pp, int aa, string versiKitab)
        {
            ObservableCollection<String> tmp = new ObservableCollection<String>();
            string k = Convert.ToString(kk);
            string p = Convert.ToString(pp);
            string a = Convert.ToString(aa);
            int x = 0;
            string tmp2 = "";
            bool startRead = false;
            bool endRead = false;
            bool ayatMazmuzrkah = ((kk == 19) && (versiKitab.Equals("TerjemahanBaru")));
            char[] delimiterChars = { '\t' };
            String tempxx = "Kitab/" + versiKitab + "/" + k + ".txt";
            var rs = Application.GetResourceStream(new Uri(tempxx, UriKind.Relative));
            StreamReader inFlow = new StreamReader(rs.Stream);
            while (startRead == false)
            {
                tmp2 = inFlow.ReadLine();

                string[] pecahan = tmp2.Split(delimiterChars);
                if (pecahan[2].Equals(p))
                {
                    if (pecahan[3].Equals(a))
                    {
                        startRead = true;
                        if (ayatMazmuzrkah) { tmp.Add("(" + pecahan[3] + ") " + Alkitab.Bacaan.prosesMazmurAyat(pecahan[4])); } else tmp.Add("(" + pecahan[3] + ") " + pecahan[4]);
                        x++;
                    }
                }
            }

            while (endRead == false)
            {
                tmp2 = inFlow.ReadLine();
                if (tmp2 == null) { endRead = true; break; }
                string[] pecahan = tmp2.Split(delimiterChars);
                if (!(pecahan[2].Equals(p))) { endRead = true; break; }
                if (ayatMazmuzrkah) { tmp.Add("(" + pecahan[3] + ") " + Alkitab.Bacaan.prosesMazmurAyat(pecahan[4])); } else tmp.Add("(" + pecahan[3] + ") " + pecahan[4]);
                x++;
            }

            return tmp;
        }

        #endregion Methods
    }
}